from typing import Any, List
import os
from datetime import timedelta, time

from celery.app.base import Celery


def get_user_settings(fields: List[str]) -> List[str]:
    """ Получаем пользовательские настройки из файла user_data/settings.py """

    user_settings: List[str] = []
    path_to_file: str = os.path.abspath('user_data/settings.txt')

    if not os.path.exists(path_to_file):
        raise ValueError('Не найдено файла user_data/settings.txt')

    with open(path_to_file) as file:
        file_content = file.read().strip()
        file_content = file_content.split(
        '-----------------------------'
        )[1].strip()

    for line in file_content.split('\n\n'):
        name, value = line.split('\n')
        if name in fields: user_settings.append(value)

    return user_settings

def get_parsing_time() -> List[int]:
    """ Получаем время парсинга """

    parsing_time: List[str] = get_user_settings(
        ['start_of_parsing', 'end_of_parsing']
    )
    parsing_time: List[int] = list(map(int, parsing_time))
    return parsing_time

def run_necessary_task(app: Celery) -> None:
    """ Запускаем необходимые задачи """

    start_of_parsing, end_of_parsing = get_parsing_time()
    app.conf.beat_schedule = {
        'track-appearance-sheepments': {
            'task': 'celery_app.track_appearance_sheepments',
            'schedule': timedelta(minutes=1),
            'options': {
                'time_start': time(start_of_parsing, 0, 0),
                'time_end': time(end_of_parsing, 0, 0),
            }
        },
    }
